<?php
namespace XWAM\Controller;

/**
 * 订单控制器
 * 
 * @author SoChishun <14507247@qq.com>
 * @since 1.0.0 <2015-4-2> SoChishun Added.
 */
class OrderController extends AppbaseController {
    
    /**
     * 检测用户访问权限
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    public function assert_upr($rule = '') {
        $upr = array(
            'order_list' => true,
            'order_edit' => true,
        );

        $this->user_permission_rules = $upr;
        if ($rule && (!array_key_exists($rule, $upr) || !$upr[$rule])) {
            die('您没有访问权限!');
        }
    }

    /**
     * 获取order_list查询表单数据
     * @return array 包含search和where数组
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    function order_list_search() {
        $search = I('get.');
        if ($search['keyword']) {
            $where['name'] = array('like', '%' . $search['keyword'] . '%');
        }
        $tab = I('tab');
        $search['tel'] = I('tel');
        $search['name'] = I('name');
        $search['sn'] = I('sn');
        $search['sellerUserName'] = I('sellerUserName');
        $search['orderAmountMin'] = I('orderAmountMin');
        $search['orderAmountMax'] = I('orderAmountMax');
        if ($tab) {
            $search['tab'] = $tab;
        }
        if ($search['tel']) {
            $where['telphone'] = $search['tel'];
        }
        if ($search['name']) {
            $where['personalName'] = $search['name'];
        }
        if ($search['sn']) {
            $where['serialNo'] = $search['sn'];
        }
        if ($search['orderAmountMin']) {
            $where['orderAmount'][] = array('EGT', $search['orderAmountMin']);
        }
        if ($search['orderAmountMax']) {
            $where['orderAmount'][] = array('ELT', $search['orderAmountMax']);
        }
        if ($search['sellerUserName']) {
            $where['sellerUserName'] = $search['sellerUserName'];
        }
        if ($this->RoleInfo['SEAT']) {
            $where['sellerID'] = $this->user_login_data['id'];
        } else if ($this->RoleInfo['ADMIN']) {
            $where['siteID'] = $this->user_login_data['siteID'];
        }
        $tpl = '';
        $table = 'vorder';
        switch ($tab) {
            case 'unconfirmed': // 待审核(状态:0=未审核)
                $tpl = 'order_list_unconfirmed_part';
                $where['state'] = 0;
                break;
            case 'returnconfirm': // 审核退回(状态:24=核单失败,34=仓库退回)
                $tpl = 'order_list_returnconfirm_part';
                $where['state'] = array('IN', '24,34');
                break;
            case 'unfilled': // 待发货(状态:20=核单通过)
                $tpl = 'order_list_unfilled_part';
                $where['state'] = 20;
                break;
            case 'delivered': // 已发货(状态:30=已发货)
                $table = 'vorder_logistics';
                $tpl = 'order_list_delivered_part';
                $where['state'] = 30;
                break;
            case 'signed': // 客户签收(状态:50=已签收)
                $table = 'vorder_logistics';
                $tpl = 'order_list_signed_part';
                $where['state'] = 50;
                break;
            case 'refused': // 拒绝签收(状态:54=客户拒签)
                $table = 'vorder_logistics';
                $tpl = 'order_list_signed_part';
                $where['state'] = 54;
                break;
            case 'returned': // 已退货(状态:55=客户退货)
                $table = 'vorder_logistics';
                $tpl = 'order_list_returned_part';
                $where['state'] = 55;
                break;
            case 'canceled': // 已取消(40=座席取消,41=买家取消,42=逾期取消,43=订单无效取消)
                $tpl = 'order_list_canceled_part';
                $where['state'] = array('BETWEEN', '40,49');
                break;
            case 'settled': // 已结算(0=未收款,1=已收款,2=部分收款)
                $tpl = 'order_list_settled_part';
                $where['orderSettlementState'] = array('GT', 0);
                break;
            case 'unsettled': // 未结算(0=未收款,1=已收款,2=部分收款)
                $tpl = 'order_list_settled_part';
                $where['orderSettlementState'] = 0;
                break;
            case 'dispatch': // 调度订单
                $tpl = 'order_list_dispatch_part';
                $where['oldSellerID'] = array('GT', 0);
                break;
        }
        return array('table' => $table, 'search' => $search, 'where' => $where, 'tpl' => $tpl);
    }

    /**
     * order_list操作
     * @since 1.0.0 <2015-4-2> SoChishun Added.
     */
    public function order_list() {
        $this->assert_upr(ACTION_NAME); // 检测访问权限
        $this->base_common_add_visit_history('订单管理'); // 添加用户浏览历史
        $search_data = $this->order_list_search(); // 获取查询表单数据
        $list = D('Order')->get_paging_list($search_data['where'], $pager);
        $this->assign('list', $list);
        $this->assign('pager', $pager);
        $this->assign('upr', $this->user_permission_rules); // 声明页面权限变量
        $this->display_tpp();
    }

    /**
     * order_edit操作
     * @param int $id 主键编号
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    public function order_edit($id = 0) {
        $this->assert_upr(ACTION_NAME); // 检测访问权限
        $this->base_common_add_visit_history($id?'编辑订单':'新增订单'); // 添加用户浏览历史
        $this->base_common_edit_view($id, 'Order');
    }

    /**
     * order_edit_save操作
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    public function order_edit_save() {
        $result = D('Order')->save_item();
        $this->base_redirect_return($result, U('Order/order_list'));
    }

    /**
     * order_delete操作
     * @param int $id 主键编号
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    public function order_delete($id = 0) {
        $this->base_common_delete($id, 'Order');
    }
}